package com.xuetang.controller.admin;

import com.xuetang.entity.AdminUser;
import com.xuetang.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员用户REST控制器
 */
@RestController
@RequestMapping("/admin/api/users")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 获取当前登录的管理员用户信息
     */
    @GetMapping("/current")
    public Map<String, Object> getCurrentUser(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            AdminUser adminUser = (AdminUser) session.getAttribute("adminUser");
            if (adminUser != null) {
                result.put("code", 0);
                result.put("message", "获取成功");
                result.put("data", adminUser);
            } else {
                result.put("code", 1);
                result.put("message", "用户未登录");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有管理员用户
     */
    @GetMapping("/admin-users")
    public Map<String, Object> listAdminUsers() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<AdminUser> adminUsers = adminUserService.listAll();
            result.put("code", 0);
            result.put("message", "获取成功");
            result.put("data", adminUsers);
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 获取管理员用户详情
     */
    @GetMapping("/admin-users/{id}")
    public Map<String, Object> getAdminUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            AdminUser adminUser = adminUserService.getById(id);
            if (adminUser != null) {
                result.put("code", 0);
                result.put("message", "获取成功");
                result.put("data", adminUser);
            } else {
                result.put("code", 1);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 添加管理员用户
     */
    @PostMapping("/admin-users")
    public Map<String, Object> addAdminUser(@RequestBody AdminUser adminUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = adminUserService.add(adminUser);
            if (success) {
                result.put("code", 0);
                result.put("message", "添加成功");
            } else {
                result.put("code", 1);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新管理员用户
     */
    @PutMapping("/admin-users/{id}")
    public Map<String, Object> updateAdminUser(@PathVariable Long id, @RequestBody AdminUser adminUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            adminUser.setId(id);
            boolean success = adminUserService.update(adminUser);
            if (success) {
                result.put("code", 0);
                result.put("message", "更新成功");
            } else {
                result.put("code", 1);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除管理员用户
     */
    @DeleteMapping("/admin-users/{id}")
    public Map<String, Object> deleteAdminUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = adminUserService.delete(id);
            if (success) {
                result.put("code", 0);
                result.put("message", "删除成功");
            } else {
                result.put("code", 1);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
}